package org.bookie.mtest.framework;

import static org.testng.Assert.assertEquals;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Object that encapsulates application in general
 * Starting point for all End2End testcases
 * 
 * @author zubairov
 *
 */
public class Application {
	
	public static final String APPLICATION_URL = "http://freebookie.org:8090/bookie";
	
	private static final String MAIN_PAGE_TITLE = Page.PAGE_TITLE_PREFIX+"Free internet bookmaker for all";

	private static final Log log = LogFactory.getLog(Application.class);

	/**
	 * Creates a new application with given URL as starting point
	 * 
	 * @param url
	 */
	public Application() {
		HttpUnitOptions.setScriptingEnabled(false);
	}
	
	/**
	 * Launching application and returning main page
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws SAXException
	 */
	public Page start() throws MalformedURLException, IOException, SAXException {
		WebConversation conversation = new WebConversation();
		WebRequest req = new GetMethodWebRequest(APPLICATION_URL);
		log.info("Starting from " + APPLICATION_URL);
		WebResponse response = conversation.getResponse(req);
		assertEquals(MAIN_PAGE_TITLE, response.getTitle());
		return new Page(response);
	}

}
